#ifndef __INPUT_HPP__
#define __INPUT_HPP__ 1

#include <vector>
#include <thread>

#include <Windows.h>

struct Event;
class EventHandler;

struct ButtonState
{
	ButtonState() : isDown(false), wasDown(false) { }
	ButtonState(bool isDown, bool wasDown) : isDown(isDown), wasDown(wasDown) { }

	bool isDown;
	bool wasDown;
};

struct KeyInput
{
	std::vector<ButtonState*> keyStates;
};

struct MouseInput
{
	long mouseX;
	long mouseY;
	long wheelDelta;
	std::vector<ButtonState*> mouseStates;
};

class CInput
{
public:
	CInput(EventHandler& eventHandler);
	~CInput();

	static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
	LRESULT CALLBACK MsgHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
	void WindowCreate();
	void RegisterDevices();
	HWND GetWindowHandle() const;
	void SetWindowHandle(HWND handle);
	int InputThread();

	size_t SwapInputBuffers();
	KeyInput* GetKeyInput();
	MouseInput* GetMouseInput();
	void ClearKeyInput();
	void ClearMouseInput();

	// Event handlers
	bool OnShutdown(Event*);

private:
	CInput operator=(const CInput&);

	HWND hWnd;
	std::thread thr;
	EventHandler& eventHandler;
	RAWINPUTDEVICE inputDevices[2];
	std::vector<KeyInput*> keyInput;
	std::vector<MouseInput*> mouseInput;

	size_t bufIdx;
};

#endif